<?php

namespace app\admin\controller\system;

use think\App;
use app\Request;
use think\response\Json;
use think\exception\ValidateException;

use app\admin\model\SystemUploadfile;
use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnnotation;

use app\admin\model\BenefitLwf;
use app\admin\model\BenefitZcf;
use app\admin\model\BenefitRcl;
use app\admin\model\BenefitLog;
use app\admin\model\BenefitWellNumber;

#[ControllerAnnotation(title: '上传文件管理')]
class Uploadfile extends AdminController
{

    #[NodeAnnotation(ignore: ['modify', 'recycle'])]
    protected array $ignoreNode;

    public function __construct(App $app)
    {
        parent::__construct($app);
        self::$model = SystemUploadfile::class;
        $this->assign('upload_types', config('admin.upload_types'));
    }

    #[NodeAnnotation(title: '列表', auth: true)]
    public function index(Request $request): Json|string
    {
        if ($request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParams();
            $count = self::$model::where($where)->count();
            $list  = self::$model::where($where)
                ->where('user', session('admin.id'))
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    #[NodeAnnotation(title: '删除', auth: true)]
    public function delete(Request $request): void
    {
        $this->checkPostRequest();
        $id  = $request->param('id');
        $row = self::$model::whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        $model = $row[0];
        $where['file'] = $model->new_name;
        BenefitLog::where($where)->delete();
        BenefitLwf::where($where)->delete();
        BenefitRcl::where($where)->delete();
        BenefitWellNumber::where($where)->delete();
        BenefitZcf::where($where)->delete();
        $path = $model->url;
        try {
            $filePath = app()->getRootPath() . 'public/' . $path;
            // // 检查文件是否存在
            if (file_exists($filePath)) {
                unlink($filePath);
            }
        } catch (ValidateException $e) {
            Log::error($e->getMessage());
        }
        try {
            $save = $model->delete();
        }catch (\Exception $e) {
            $this->error(config("code.operation_failed"));
        }
        $save ? $this->success(config("code.operation_success")) : $this->error(config("code.operation_failed"));
    }
    
    #[NodeAnnotation(title: '效益', auth: true)]
    public function benefit(Request $request, $id = 0): string
    {
        $row = self::$model::find($id);
        empty($row) && $this->error('数据不存在');
        return $this->fetch();
    }
}
